<?php

/**
 * This is the model class for table "donation".
 *
 * The followings are the available columns in table 'donation':
 * @property integer $id
 * @property string $donation_type
 * @property string $donation_status
 * @property string $amount
 * @property string $donation_date
 * @property string $receipt
 * @property integer $user_id
 *
 * The followings are the available model relations:
 * @property Cheque[] $cheques
 * @property User $user
 * @property Paypal[] $paypals
 */
class Donation extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Donation the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'donation';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('donation_type, donation_status, amount, donation_date, receipt, user_id', 'required'),
			array('user_id', 'numerical', 'integerOnly'=>true),
			array('donation_type, donation_status, receipt', 'length', 'max'=>45),
			array('amount', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, donation_type, donation_status, amount, donation_date, receipt, user_id', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cheques' => array(self::HAS_MANY, 'Cheque', 'cheque_donation_id'),
			'user' => array(self::BELONGS_TO, 'User', 'user_id'),
			'paypals' => array(self::HAS_MANY, 'Paypal', 'paypal_donation_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'donation_type' => 'Type',
			'donation_status' => 'Status',
			'amount' => 'Amount',
			'donation_date' => 'Date',
			'receipt' => 'Receipt',
			'user_id' => 'User',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('donation_type',$this->donation_type,true);
		$criteria->compare('donation_status',$this->donation_status,true);
		$criteria->compare('amount',$this->amount,true);
		$criteria->compare('donation_date',$this->donation_date,true);
		$criteria->compare('receipt',$this->receipt,true);
		$criteria->compare('user_id',$this->user_id);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function getName()
	{
		return $this->user->fullName;
	}

	public function getStatus()
	{
		return array('pending'=>'pending','approved'=>'approved');
	}
	public function getDonationType()
	{
		return array('cash'=>'cash','cheque'=>'cheque','paypal'=>'paypal');
	}

		public function Amount(){
		setlocale(LC_MONETARY, 'en_US');
		if($this->amount != null){
				return 'Php '. number_format($this->amount,2);
		}else if($this->cheques != null){
				return 'Php '.number_format($this->amount,2);
		}else if($this->paypals != null){
				return 'Php '. number_format($this->amount,2);
		}else{
				return 'Php '. number_format(0,2);
		}
	}
}